<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){	
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','goods_id','end_price','start_time','status','end_time','initiate');
	$list=pdo_getslice('jy_bargain_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('price','title','image','total','status'));
			$item['price']=$goods['price'];
			$item['total']=$goods['total'];
			$item['title']=$goods['title'];
			$item['start_time']=date('Y-m-d H:i:s',$item['start_time']);
			$item['end_time']=date('Y-m-d H:i:s',$item['end_time']);
			$item['image']=$goods['image'];
			$item['status']=$goods['status'];
		}
		
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'list2','title'=>'添加砍价活动'),
		),
		'list'=>array('data'=>'list','del_one'=>true,'delall'=>false,'list'=>array(		
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'price','title'=>'标价','type'=>'string'),
			array('name'=>'end_price','title'=>'底价','type'=>'string'),
			array('name'=>'start_time','title'=>'开始时间','type'=>'string'),
			array('name'=>'end_time','title'=>'结束时间','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('name'=>'initiate','title'=>'发起次数','type'=>'string'),
			array('name'=>'status','title'=>'商品状态','type'=>'radio','options'=>'status'),			
		),
	),
	);
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	$return['htmls']=$htmls;
	exi($return);
}
//添加砍价活动
if($_RQ['op']=='list2'){
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	$return['status2']=array('0'=>'可参与','1'=>'含规格不支持','2'=>'已参与砍价','3'=>'已参与其他活动');
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']='1';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','image','price','status','createtime');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['gid']=$item['id'];
			$gg=pdo_get('jy_weishop_goods_spec',array('goodsid'=>$item['id']));
			$is=pdo_get('jy_bargain_goods',array('goods_id'=>$item['id']));
			$item['status2']='0';
			if($gg){
				$item['gg']=1;
				$item['status2']='1';
			}
			if($is){
				$item['status2']='2';
			}
			if($this->plugin('jy_weishop')->check_active($item['id'])){
				$item['status2']='3';
			}
			if(!$gg && !$is){
				$item['gg']=0;
			}
		}
		
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'list2','title'=>'添加砍价活动'),
		),
		'list'=>array('tip'=>'搜索需要创建砍价活动的商品，点击右侧“参加砍价”按钮！','data'=>'list','key'=>'gid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
		
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('name'=>'status','title'=>'商品状态','type'=>'radio','options'=>'status'),
			array('name'=>'status2','title'=>'参与情况','type'=>'radio','options'=>'status2'),			
		),
		'action'=>array(
			array('op'=>'edit','title'=>'参加砍价','type'=>'href','show'=>array('status2'=>0)),
		)
		),
		'search_one'=>array('name'=>'title','title'=>'商品名称','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['id']){
		$id=$_RQ['id'];
		$item=pdo_get('jy_bargain_goods',array('id'=>$id));
		$item['probability']=json_decode($item['probability'],true);
		$item['time']=array('tstart'=>$item['start_time'],'tend'=>$item['end_time']);
		$good=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','price'));
		$item['price']=$good['price'];
		$item['title']=$good['title'];
	}
	if($_RQ['gid']){
		$gid=$_RQ['gid'];
		$item=pdo_get('jy_weishop_goods',array('id'=>$gid),array('title','price'));
		$item['goods_id']=$gid;
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'砍价设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活动列表')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','read'=>true),
			array('name'=>'price','title'=>'商品标价','type'=>'string','read'=>true),
			array('name'=>'end_price','title'=>'商品底价','type'=>'string'),
			array('name'=>'type','title'=>'显示底价','type'=>'radio','options'=>'type'),
			array('name'=>'myself','title'=>'自己砍价','type'=>'radio','options'=>'myself'),
			array('name'=>'show','title'=>'没到底价','type'=>'radio','options'=>'show'),
			array('name'=>'time','title'=>'活动时间','type'=>'time_range'),
			array('name'=>'time_limit','title'=>'砍价时限','type'=>'string','help'=>'单位：小时,0为不限时'),
			array('name'=>'total_time','title'=>'可砍价总次数','type'=>'string','edit'=>false),
			array('name'=>'each_time','title'=>'每人可砍次数','type'=>'string','edit'=>false),
			array('name'=>'maximum','title'=>'活动可发起次数','type'=>'string'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'array','name'=>'probability','title'=>'设置砍价规则，示例：起设置为1，终设置为2，概率设置为10，表示，砍价在1-2元的概率为10%','add_tip'=>'添加一个价格区间',
				'list'=>array(
					array('title'=>'起','name'=>'start','type'=>'string'),	
					array('title'=>'终','name'=>'end','type'=>'string'),
					array('title'=>'概率','name'=>'jilv','type'=>'string'),						
				),
				'details'=>array(
					array('title'=>'起','name'=>'start','type'=>'string','help'=>'设置每次砍去价格的区间和概率'),	
					array('title'=>'终','name'=>'end','type'=>'string'),
					array('title'=>'概率','name'=>'jilv','type'=>'string','help'=>'此处只需填写数字，各区间总数必须为100'),			
				)
				),
			)
	);
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	$return['type']=array('0'=>'不显示','1'=>'显示');
	$return['myself']=array('0'=>'禁止','1'=>'允许');
	$return['show']=array('0'=>'不能下单','1'=>'可以下单');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['probability']){
		exi('请设置砍价区间','error');
	}
	$p=0;
	foreach($postdata['probability'] as $v){
		$p +=$v['jilv'];
	}
	if($p!=100){
		exi('几率之和必须为100','error');
	}
	if(!$postdata['total_time']){
		exi('请设置总砍价次数','error');
	}
	if(!$postdata['each_time']){
		exi('请设置每人砍价次数','error');
	}
	if(!$postdata['time']){
		exi('请设置活动时间','error');
	}
	$data['status']=$postdata['status'];
	$data['start_time']=$postdata['time']['tstart'];
	$data['end_time']=$postdata['time']['tend'];
	$data['probability']=json_encode($postdata['probability']);
	$data['end_price']=$postdata['end_price'];
	$data['show']=$postdata['show'];
	$data['myself']=$postdata['myself'];
	$data['type']=$postdata['type'];
	$data['time_limit']=$postdata['time_limit'];
	$data['total_time']=$postdata['total_time'];
	$data['each_time']=$postdata['each_time'];
	$data['maximum']=$postdata['maximum'];
	if($postdata['id']){
		pdo_update('jy_bargain_goods',$data,array('id'=>$postdata['id']));
		exi('保存成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['goods_id']=$postdata['goods_id'];
		if($this->plugin('jy_weishop')->check_active($data['goods_id'])){
			exi('添加失败，该商品已存在活动！','error');
		}
		pdo_insert('jy_bargain_goods',$data);
		$id=pdo_insertid();
		$this->plugin('jy_weishop')->create_active($data['goods_id'],$id,$this->plugin_name);
		exi('添加成功','','list');
	}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_bargain_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$g=pdo_get('jy_bargain_goods',array('id'=>$id),array('goods_id','id'));
				$this->plugin('jy_weishop')->del_active($g['goods_id'],$id,$this->plugin_name);
				pdo_delete('jy_bargain_goods',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}